﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PEC.Forms
{
    public partial class FrmLenhSX : Form
    {
        PecController _pecController;
        string sErr = "";
        
        public FrmLenhSX(PecController pec)
        {
            _pecController = pec;
            InitializeComponent();
        }



        private void SetData(DTO.OpLenhSXInfo info)
        {
            txtLenhSX.Text = info.LenhSX;
            txtName.Text = info.Name;
            txtStatus.Text = info.Status;
        }



        private DTO.OpLenhSXInfo GetValue()
        {
            DTO.OpLenhSXInfo info = new DTO.OpLenhSXInfo();
            try
            {
                info.LenhSX = txtLenhSX.Text.Trim();
                info.Name = txtName.Text.Trim();
                info.Status = txtStatus.Text.Trim();
            }
            catch(Exception ex) 
            {
                MessageBox.Show(ex.Message);
            }
            return info;
        }

        private void btSave_Click(object sender, EventArgs e)
        {
            DTO.OpLenhSXInfo info;
            
            try
            {
                info = GetValue();
                BUS.OpLenhSXControl ctr = new BUS.OpLenhSXControl(_pecController.Bus);
                ctr.InsertUpdate(info);
                LoadGrid();
                MessageBox.Show("Save Successful");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void FrmLenhSX_Load(object sender, EventArgs e)
        {
            LoadGrid();
        }

        private void LoadGrid()
        {
            BUS.OpLenhSXControl ctr = new BUS.OpLenhSXControl(_pecController.Bus);
            opLenhSXInfoBindingSource.DataSource = ctr.GetByFilterToDataTable(null, ref sErr);
        }

        private void dgvList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex >= 0)
            {
                DTO.OpLenhSXInfo info = new DTO.OpLenhSXInfo((DataRowView)dgvList.Rows[e.RowIndex].DataBoundItem);
                SetData(info);
            }
        }

    }
}
